import com.alibaba.fastjson.JSON;

import java.io.*;

public class main {

    public static void main(String[] args) {
        String path = "/Users/suxiaomeng/Desktop/55246614";    //要遍历的路径
        File file = new File(path);        //获取其file对象
        func(file);
    }

    //遍历文件目录
    public static void func(File file) {
        File[] fs = file.listFiles();
        //新的文件路径基础路径
        String newPath = "/Users/suxiaomeng/Study/视频/黑马Java基础+就业班+各种项目idea版本（正在更新）/";
        for (File f : fs) {
            if (f.isDirectory())    //若是目录，则递归打印该目录下的文件
                func(f);
            if (f.isFile() && f.getName().endsWith(".blv")){//打印.blv格式文件
                System.out.println(f);
                moveFile(f.getPath(),newPath+f.getName());
            }
        }
    }

    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //dirPath 文件夹磁盘路径 
    private static void isChartPathExist(String dirPath) {

        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        } else {
            System.out.println(dirPath + " 该文件夹已经存在！");
        }
    }

    //移动文件
    public static void moveFile(String fromPath, String toPath){
//        fromPath="D:\\test\\1.mp4";
//        toPath="D:\\totest";
        System.out.println("移动文件：从路径 " + fromPath + " 移动到路径 " + toPath);
        File file = new File(fromPath);
        if (file.isFile()){
            File toFile=new File(toPath);
            if (toFile.exists()){
                System.out.println("文件已存在");
            }
            else{
                file.renameTo(toFile);
                System.out.println("移动文件成功");
            }
        }
    }
}

